/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import montecarlo.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import model.Options;

/**
 *
 * @author Gautier & Sacha
 */
public class Window extends JFrame implements ActionListener{
    private Options options[];
    private JComboBox option;
    private JComboBox pull;
    
    private JLabel l_nbSteps;
    private JTextField t_nbSteps;
    private JLabel l_nbSimu;
    private JTextField t_nbSimu;
    
    private JLabel time;
    private JLabel cost;
    private JLabel volatility;
    private JLabel rateLabel;
    private JLabel actionPrice;
    private JLabel strikePrice;
    
    private JProgressBar progress;
    private JLabel result;
    
    private JButton start;
    private JButton multithread;
    
    private JPanel cont;
    
    public Window(Options opt[]){
        super("Monte Carlo");
        options=opt;
        this.setPreferredSize(new Dimension(300, 100));
        this.setSize(508, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Mise en page
        cont= new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx=0;
        c.gridy=0;
        c.gridheight=1;
        c.gridwidth=1;
        c.ipadx=2;
        c.ipady=2;
        
        String[] optionStrings = { "Option 1", "Option 2","Option 3","Option 4","Option 5"};
        option = new JComboBox(optionStrings);
        option.addActionListener(this);
        cont.add(option,c);
        
        c.gridx=1;
        c.gridy=0;
        String[] actionStrings = { "Call", "Put"};
        pull = new JComboBox(actionStrings);
        cont.add(pull,c);
        c.gridx=2;
        c.gridy=0;
        l_nbSteps = new JLabel("Nb steps :");
        cont.add(l_nbSteps,c);
        c.gridx=3;
        c.gridy=0;
        t_nbSteps = new JTextField(4);
        cont.add(t_nbSteps,c);
        c.gridx=4;
        c.gridy=0;
        l_nbSimu = new JLabel("Nb simus :");
        cont.add(l_nbSimu,c);
        c.gridx=5;
        c.gridy=0;
        t_nbSimu = new JTextField(4);
        cont.add(t_nbSimu,c);
        
        c.gridx=2;
        c.gridy=1;
        start = new JButton("Single Thread");
        start.addActionListener(this);
        cont.add(start,c);
        
        c.gridx=4;
        c.gridy=1;
        multithread = new JButton("Multithread");
        multithread.addActionListener(this);
        cont.add(multithread,c);
        
        //Labels
        c.gridx=0;
        c.gridy=2;
        c.gridheight=1;
        c.gridwidth=2;
        actionPrice= new JLabel("Action price :"+opt[0].getS());
        cont.add(actionPrice,c);
        c.gridx=2;
        c.gridy=2;
        strikePrice= new JLabel("Action price :"+opt[0].getX());
        cont.add(strikePrice,c);
        c.gridx=0;
        c.gridy=3;
        time= new JLabel("Time :"+opt[0].getT());
        cont.add(time,c);
        c.gridx=2;
        c.gridy=3;
        cost= new JLabel("Cost :"+opt[0].getB());
        cont.add(cost,c);
        c.gridx=0;
        c.gridy=4;
        volatility= new JLabel("Volatility :"+opt[0].getV());
        cont.add(volatility,c);
        c.gridx=2;
        c.gridy=4;
        rateLabel= new JLabel("Rate :"+opt[0].getR());
        cont.add(rateLabel,c);
        
        c.gridx=4;
        c.gridy=4;
        c.gridwidth=1;
        result=new JLabel();
        cont.add(result,c);
        
        c.gridy=3;
        cont.add(new JLabel("Result :"), c);
        
        this.add(cont);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== start){
            /*progress = new JProgressBar(0, Integer.parseInt(t_nbSimu.getText()));
            progress.setValue(0);
            progress.setStringPainted(true);
            cont.add(progress);*/
            double start, end;
            start = System.currentTimeMillis();
            result.setText(String.valueOf(MonteCarlo.MonteCarloSimulation((String)pull.getItemAt(pull.getSelectedIndex()), 
                    options[option.getSelectedIndex()], 
                    Integer.parseInt(t_nbSteps.getText()), 
                    Integer.parseInt(t_nbSimu.getText()))));
            end=System.currentTimeMillis();
            System.out.println("Time :"+(end-start));
        }
          if(e.getSource()== multithread){
            double start, end;
            start = System.currentTimeMillis();
            try {
                MonteCarloMT mt = new MonteCarloMT();
                result.setText(String.valueOf(mt.computeMonteCarlo((String) pull.getItemAt(pull.getSelectedIndex()), options[option.getSelectedIndex()], Integer.parseInt(t_nbSteps.getText()), Integer.parseInt(t_nbSimu.getText()))));
            } catch (InterruptedException ex) {
                Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
            }
            end=System.currentTimeMillis();
            System.out.println("Time :"+(end-start));
        }
        if(e.getSource()==option){
            actionPrice.setText("Action price :"+options[option.getSelectedIndex()].getS());
            strikePrice.setText("Strike price :"+options[option.getSelectedIndex()].getX());
            time.setText("Time :"+options[option.getSelectedIndex()].getT());
            cost.setText("Cost:"+options[option.getSelectedIndex()].getB());
            volatility.setText("Volatility :"+options[option.getSelectedIndex()].getV());
            rateLabel.setText("Rate :"+options[option.getSelectedIndex()].getR());
        }
    }
    
}
